<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Wacategory as WacategoryModel;
use app\common\model\Wenan as WenanModel;

class Wenan extends AdminBase
{
    protected $wacategory_model;
    protected $wenan_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wacategory_model = new WacategoryModel();
        $this->wenan_model = new WenanModel();
        $category_level_list = $this->wacategory_model->getLevelList();
        $this->assign('category_level_list',$category_level_list);
    }

    public function index($page = 1 , $cid = 0 , $keyword = ''){
        $map = [
            'status' => ['eq',1],
        ];
        if(!empty($keyword)){
            $map['title'] = ['like','%'.$keyword.'%'];
        }

        if($cid > 0){
            $map['wid'] = ['eq',$cid];
        }
        $result = $this->wenan_model->alias('w')
                    ->field('w.*,think_wacategory.name')
                    ->where($map)->join('think_wacategory','w.wid = think_wacategory.id')
                    ->order('w.examine asc,w.id desc , w.sort desc')
                    ->paginate(15,false,['page'=>$page,'query' => ['keyword' => $keyword, 'cid' => $cid]]);
        $this->assign('wenan_list',$result);
        return $this->fetch('index',[
            'keyword' => $keyword,
            'cid' => $cid,
        ]);
    }

    public function add(){
        return $this->fetch();
    }

    public function save(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $zip = new \ZipArchive();
            $zip->open(ROOT_PATH.'public'.$data['compress']);
            $wenanname = $zip->getNameIndex(0);//获取目录名称
            $wenan_path = ROOT_PATH.'public/uploads/wenan/'.$data['wid'];//文案目录
            if (!$zip->extractTo($wenan_path)) {
                $this->error('解压失败,请检查问题');
            }else{
                $zip->close();
                $data['wenan_path'] = substr($wenanname,0,-1);
                $validate_result = $this->validate($data,'Wenan');
                if($validate_result !== true){
                    $this->error($validate_result);
                }else{
                    $data['wenan_path'] = '/uploads/wenan/'.$data['wid'].'/'.$wenanname;
                    if($this->wenan_model->allowField(true)->save($data)){
                        $this->success('保存成功');
                    }else{
                        $this->error('保存失败');
                    }
                }
            }
        }
    }   

    public function edit($id){
        $result = $this->wenan_model->find($id);
        $this->assign('wenan',$result);
        return $this->fetch();
    }

    public function update($id){
        if($this->request->isPost()){
            $data = $this->request->param();
            $zip = new \ZipArchive();
            $zip->open(ROOT_PATH.'public'.$data['compress']);
            $wenanname = $zip->getNameIndex(0);//获取目录名称
            $wenan_path = ROOT_PATH.'public/uploads/wenan/'.$data['wid'];//文案目录
            if (!$zip->extractTo($wenan_path)) {
                $this->error('解压失败,请检查问题');
            }else{
                $zip->close();
                $data['wenan_path'] = '/uploads/wenan/'.$data['wid'].'/'.$wenanname;
                if($this->wenan_model->allowField(true)->save($data,$id)){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }
        }
    }

    public function delete($id){
        $result = $this->wenan_model->where('id',$id)->update(['status'=>0]);
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }


    /**
     * 获取下级分类菜单
     */
    public function getChildMenu($id){
        $result = $this->wacategory_model->field('id,name')->where('pid',$id)->select();
        return json($result);
    }

    /**
     * 审核
     */
    public function toggle(){
        $data = $this->request->param();
        if($data['type'] == 'audit'){
            //审核通过
            $ids = implode(',',$data['ids']);
            $result = $this->wenan_model->where('id','in',$ids)->update(['examine'=>1]);
            if($result){
                $this->success('审核已通过');
            }else{
                $this->error('审核失败');
            }
        }elseif($data['type'] == 'cancel_audit'){
            //审核不通过
            $ids = implode(',',$data['ids']);
            $result = $this->wenan_model->where('id','in',$ids)->update(['examine'=>2]);
            if($result){
                $this->success('审核已拒绝');
            }else{
                $this->error('审核失败');
            }
        }
    }

    public function preview($id){
        $content = $this->wenan_model->where('id',$id)->value('content');
        $this->assign('content',$content);
        return $this->fetch();  
    }
}